package com.zzl.boot.bigevent.mapper;

import com.zzl.boot.bigevent.pojo.article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface articleMapper {
    //添加文章
    @Select("insert into article(title,content,cover_img,category_id,state,user_id,create_time,update_time)" +
            "values (#{title},#{content},#{coverImg},#{categoryId},#{state},#{userId},now(),now())")
    void addArticle(article article);
    //分页获取文章
    List<article> getClassPages(Integer userId, Integer categoryId, String state);
    //根据文章id获取文章详情
    @Select("select * from article where id=#{id}")
    article getById(Integer id);
    //更新文章信息
    @Select("update article set title=#{title},content=#{content}" +
            ",cover_img=#{coverImg},category_id=#{categoryId},state=#{state},update_time=now()" +
            "where id=#{id}")
    void update(article article);
    @Select("delete from article where id=#{id}")
    void delById(Integer id);
    //根据分类删除
    @Select("delete from article where category_id=#{categoryId} and user_id=#{userId}")
    void delByCate(Integer categoryId, Integer userId);

    @Select("select max(id) from article where user_id=#{userId}")
    Integer getArcId(Integer userId);

    @Select("select * from article where user_id=#{userId}")
    List<article> listByUser(Integer id);
    //用于展示主页文章列表
    @Select("select * from article  where state='已发布' order by create_time desc ,visit_count desc")
    List<article> getList();

    @Select("select count(*) from article where category_id=#{id}")
    Integer getCategoryCount(Integer id);

    @Select("select * from article where category_id=#{categoryId}")
    List<article> listByCategory(Integer categoryId);

    @Select("update article set visit_count=visit_count+1 where id=#{id}")
    void addView(Integer id);

    //搜索文章
    @Select("select * from article where title like #{keyword} ORDER BY visit_count DESC limit 5")
    List<article> search(String keyword);

    @Select("select user_name from user where id " +
            "= (select user_id from article where id=#{id})")
    String findUser(Integer id);
}
